package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.FreemarkUtil;
import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/regions")
public class RegionController {
    @Autowired
    private IRegionService regionService ;
    @Autowired
    private IStrategyService strategyService ;
    @Autowired
    private ServletContext ctx ;

    /**
     * 获取某个区域下面的攻略
     * 参数：
     * 资源：/regions/{strategyId}/strategys
     * 动作：GET
     */
    @GetMapping(value = "/{regionId}/strategys",produces = "text/html;charset=utf-8")
    public void queryStrategys(StrategyQueryObject qo, HttpServletResponse resp) throws Exception {
        String dir = ctx.getRealPath("/template");
        Map<String,Object> map = new HashMap<>();
        map.put("id",qo.getRegionId());


        //全部数据
        qo.setState(-1);
        qo.setPageSize(2);
        PageInfo<Strategy> pageInfo = strategyService.query(qo);
        if (qo.getCurrentPage()!=1) {
            map.put("info",pageInfo);
            FreemarkUtil.witerDate(dir,"classify.ftl",map,resp.getWriter());
            return ;
        }
        //热门的
        qo.setState(2);
        qo.setPageSize(0);
        List<Strategy> commends = strategyService.query(qo).getList();


        map.put("commends",commends);
        map.put("info",pageInfo);
        FreemarkUtil.witerDate(dir,"strategyContent.ftl",map,resp.getWriter());



    }


    /**
     * 获取所有的旅游区域
     * 参数：无
     * 动作：get
     * 资源：/regions
     */
    @GetMapping
    public Object list(Integer state){
        //查询到所有的区域
        List<Region> list = regionService.listAll(state);
        //返回所有的数据即可
        return list ;
    }
}
